#!/usr/bin/python
import os, pygame
import predef, guifaimage, guifasound, board, chessman, game, guifawidget
from pygame.locals import *
from predef import *
from guifaimage import *
from board import *
from chessman import *

class SpriteCur():
    def __init__(self, img1, img2):
        self.image1 = img1
        self.image2 = img2
        self.image = self.image1
        self.rect = self.image.get_rect()

    def cur1(self):
        self.image = self.image1

    def cur2(self):
        self.image = self.image2

    def draw(self, screen):
        screen.blit(self.image, self.rect)


#init
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((WINDOW_W, WINDOW_H), pygame.DOUBLEBUF, 32)
pygame.display.set_caption("guifa")
sounddic = guifasound.GuifaSound()
imagedic = guifaimage.GuifaImage()
clock = pygame.time.Clock()
running = True

def create_skillselector():
    global imagedic
    ss = guifawidget.SkillSelector(748, 150, 65, 500)
    
    for skill in SKILLLIST:
        node = guifawidget.SkillNode(skill)
        node.img_enable = imagedic.get_image(skill + ".png")
        node.img_disable = imagedic.get_image(skill + "_na.png")
        ss.append_node(node)

    return ss

try:
    
    gfgame = game.GuifaGame(imagedic, sounddic)

    #order is important, adding sceptre before adding forcefielder
    a = 6
    for k in KINDLIST:
        gfgame.add_live(PLAYER1, k, 3+a, 11)
        gfgame.add_live(PLAYER2, k, 11, 9-a)
        gfgame.add_live(PLAYER3, k, 9-a, 1)
        gfgame.add_live(PLAYER4, k, 1, 3+a)
        a = a - 1
    backimage = imagedic.get_image("board.png")
    indicatorimages = [] + \
            [imagedic.get_image("blue_sceptre.png")] + \
            [imagedic.get_image("brown_sceptre.png")] + \
            [imagedic.get_image("green_sceptre.png")] + \
            [imagedic.get_image("yellow_sceptre.png")]

    indicator = guifawidget.PlayerIndicator(730, 0, 100, 100, indicatorimages)
    cancelbutton = guifawidget.Button(748, 150, 65, 65, imagedic.get_image("button_cancel.png"))
    skillsel = create_skillselector()
    curmouse = SpriteCur(imagedic.get_image("cur.png"), imagedic.get_image("cur3.png"))
    #cur_group = pygame.sprite.Group()
    #cur_group.add(curmouse)
except:
    print "Init failed"
    running = False


while running:
    pygame.event.pump()

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            elif event.key == K_n:
                gfgame.next_player()
                indicator.set_player(gfgame.get_current_player())
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                if gfgame.action_state == ACTION_SELECT_CORPSE:
                    selected_corpse = gfgame.corpsesel.on_mouse_click(pygame.mouse.get_pos())
                    if selected_corpse != None:
                        gfgame.action_select_corpse(selected_corpse)
                        gfgame.next_player()
                        indicator.set_player(gfgame.get_current_player())
                        skillsel.reset()
                        cancelbutton.hide()
                        curmouse.cur1()
                        continue
                x, y = screen_to_board(event.pos[0], event.pos[1])
                # player selector
                selplayer = indicator.on_mouse_click(pygame.mouse.get_pos())
                if selplayer != None:
                    gfgame.set_player(selplayer)
                    indicator.set_player(selplayer)

                # board
                if is_inboard(event.pos[0], event.pos[1]):
                    if gfgame.click_board(x, y):
                        gfgame.next_player()
                        indicator.set_player(gfgame.get_current_player())

                # skill selector
                skillsel.reset()
                if gfgame.cur_selected_chess != None and gfgame.action_state == ACTION_MOVE:
                    m, n = screen_to_board(gfgame.cur_selected_chess.rect[0],
                            gfgame.cur_selected_chess.rect[1])
                    if gfgame.cur_selected_chess.kind == KIND_SUMMONER:
                        skillsel.set_show(SKILL_SUMMON_AD, True)
                        skillsel.set_show(SKILL_SUMMON, True)
                        if not gfgame.cur_selected_chess.upkeep:
                            if is_center(m, n):
                                skillsel.set_enable(SKILL_SUMMON_AD, True)
                            elif not gfgame.cur_selected_chess.inhibited and \
                                    not gfgame.cur_selected_chess.seal:
                                skillsel.set_enable(SKILL_SUMMON, True)
                    elif gfgame.cur_selected_chess.kind == KIND_ILLUSIONIST \
                            or gfgame.cur_selected_chess.kind == KIND_CLONE:
                        skillsel.set_show(SKILL_EXCHANGE, True)
                        skillsel.set_show(SKILL_CLONE, True)
                        if not gfgame.cur_selected_chess.inhibited and \
                                not gfgame.cur_selected_chess.seal:
                            skillsel.set_enable(SKILL_EXCHANGE, True)
                        if is_center(m, n):
                            skillsel.set_enable(SKILL_CLONE, True)
                    elif gfgame.cur_selected_chess.kind == KIND_SORCERER:
                        skillsel.set_show(SKILL_DISMEMBER, True)
                        skillsel.set_show(SKILL_CRUSH_AD, True)
                        if not gfgame.cur_selected_chess.inhibited and \
                                not gfgame.cur_selected_chess.seal and \
                                gfgame.have_corpse(m, n):
                            skillsel.set_enable(SKILL_DISMEMBER, True)
                        if is_center(m, n):
                            skillsel.set_enable(SKILL_CRUSH_AD, True)
                    elif gfgame.cur_selected_chess.kind == KIND_SOOTHSAYER:
                        skillsel.set_show(SKILL_SEAL, True)
                        skillsel.set_show(SKILL_ENCHANTMENT_AD, True)
                        if is_center(m, n):
                            skillsel.set_enable(SKILL_SEAL, True)
                            skillsel.set_enable(SKILL_ENCHANTMENT_AD, True)
                    elif gfgame.cur_selected_chess.kind == KIND_NECROMANCER:
                        skillsel.set_show(SKILL_RESURGENCE, True)
                        skillsel.set_show(SKILL_RESURGENCE_AD, True)
                        if is_center(m, n):
                            skillsel.set_enable(SKILL_RESURGENCE_AD, True)
                        elif not gfgame.cur_selected_chess.inhibited and \
                                not gfgame.cur_selected_chess.seal:
                            skillsel.set_enable(SKILL_RESURGENCE, True)
                elif gfgame.action_state != ACTION_MOVE:
                    if cancelbutton.on_mouse_click(pygame.mouse.get_pos()):
                        if gfgame.action_state == ACTION_SUMMON or gfgame.action_state == ACTION_SUMMON_AD:
                            if len(gfgame.action_chess.children) > 0:
                                gfgame.next_player()
                                indicator.set_player(gfgame.get_current_player())
                        gfgame.set_action_state(ACTION_MOVE, None)

                selected_skill = skillsel.on_mouse_click(pygame.mouse.get_pos())
                if selected_skill != None:
                    if selected_skill == SKILL_SUMMON:
                        gfgame.set_action_state(ACTION_SUMMON, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_SUMMON_AD:
                        gfgame.set_action_state(ACTION_SUMMON_AD, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_EXCHANGE:
                        gfgame.set_action_state(ACTION_EXCHANGE, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_CLONE:
                        gfgame.set_action_state(ACTION_CLONE, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_CRUSH_AD:
                        gfgame.set_action_state(ACTION_CRUSH_AD, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_DISMEMBER:
                        m, n = screen_to_board(gfgame.cur_selected_chess.rect[0],
                                gfgame.cur_selected_chess.rect[1])
                        gfgame.dismember_corpse(m, n)
                    elif selected_skill == SKILL_SEAL:
                        gfgame.set_action_state(ACTION_SEAL, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_ENCHANTMENT_AD:
                        gfgame.set_action_state(ACTION_ENCHANTMENT_AD, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_RESURGENCE:
                        gfgame.set_action_state(ACTION_RESURGENCE, gfgame.cur_selected_chess)
                    elif selected_skill == SKILL_RESURGENCE_AD:
                        gfgame.set_action_state(ACTION_RESURGENCE_AD, gfgame.cur_selected_chess)

                if gfgame.action_state != ACTION_MOVE:
                    skillsel.reset()
                    cancelbutton.show()
                    curmouse.cur2()
                else:
                    cancelbutton.hide()
                    curmouse.cur1()
                    
            elif event.button == 3:
                x, y = screen_to_board(event.pos[0], event.pos[1])
                for c in gfgame.board.chesslist(x, y):
                    print c.type
                if gfgame.action_state != ACTION_MOVE:
                    if gfgame.action_state == ACTION_SUMMON or gfgame.action_state == ACTION_SUMMON_AD:
                        if len(gfgame.action_chess.children) > 0:
                            gfgame.next_player()
                            indicator.set_player(gfgame.get_current_player())
                    gfgame.set_action_state(ACTION_MOVE, None)
                    skillsel.reset()
                    cancelbutton.hide()
                    curmouse.cur1()
        elif event.type == MOUSEMOTION:
            newx = event.pos[0] // CHESSSIZE * CHESSSIZE
            newy = event.pos[1] // CHESSSIZE * CHESSSIZE
            if newx < CHESSSIZE*11 and newy < CHESSSIZE*11:
                curmouse.rect[0] = newx
                curmouse.rect[1] = newy

    screen.blit(backimage, backimage.get_rect())
    gfgame.draw(screen)
    indicator.draw(screen)
    skillsel.draw(screen)
    cancelbutton.draw(screen)
    curmouse.draw(screen)
    pygame.display.flip()

    clock.tick(100)
    
    #end while

#finalize
pygame.font.quit()
pygame.quit()

